﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace GJXQGame
{
    

    public class ChessDataControl
    {
        /// <summary>
        /// 上次棋盘底层数据
        /// </summary>
        public DateModelControl.DateType[,] mLastMapFirst;
        /// <summary>
        /// 上次棋盘Obj
        /// </summary>
        public GameObject[,] mLastMapObj = new GameObject[8, 8];


        public List<GameObject> ListMyChess =new List<GameObject>();
        public void InitData(Act1Base actbase)
        {
            mLastMapFirst = new DateModelControl.DateType[8, 8];
            foreach (DateModelControl.ChessDate item in actbase.MyChessData)
            {
                mLastMapFirst[item.point.Point_x, item.point.Point_y].power = item.datetype.power;
                mLastMapFirst[item.point.Point_x, item.point.Point_y].type = item.datetype.type;
            }
            foreach (DateModelControl.ChessDate item in actbase.OtherChessData)
            {
                mLastMapFirst[item.point.Point_x, item.point.Point_y].power = item.datetype.power;
                mLastMapFirst[item.point.Point_x, item.point.Point_y].type = item.datetype.type;
            }
        }

        public void GetMyChess()
        {
            ListMyChess.Clear();
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    GameObject Obj = GameManager.GUIManager[GUI_ID.GUI_ID_PLAYINFOWIN].GameObject.transform.FindChild("PlayInfoWin/PlayDataObj/ChessPiecesObj").gameObject;
                    for (int index = 0; index < Obj.transform.GetChildCount(); index++)
                    {
                        if (Obj.transform.GetChild(index).GetComponent<ChessPieseSlot>().Type==TypePlay.My)
                        {
                            ListMyChess.Add(Obj.transform.GetChild(index).gameObject);
                        }
                    }
                }   
            }
            if (ListMyChess.Count<=0)
            {
                GameManager.ChessControl.ShowTitle("挑战失败");
            }
        }

        public void GetOtherChess()
        {
            bool IsOther = false;
            bool IsbHaveChange = false;
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    GameObject Obj = GameManager.GUIManager[GUI_ID.GUI_ID_PLAYINFOWIN].GameObject.transform.FindChild("PlayInfoWin/PlayDataObj/ChessPiecesObj").gameObject;
                    for (int index = 0; index < Obj.transform.GetChildCount(); index++)
                    {
                        if (Obj.transform.GetChild(index).GetComponent<ChessPieseSlot>().Type == TypePlay.Other && !IsbHaveChange)
                        {
                           IsOther = true;
                           IsbHaveChange= Obj.transform.GetChild(index).GetComponent<ChessPieseSlot>().OtherChange();
                        }
                    }
                }
            }
            if (!IsOther)
            {
                GameManager.ChessControl.ShowTitle("挑战成功");
            }
        }

        public void SetDataChange(int x,int y,int Data,int _x,int _y,GameObject Obj)
        {
            if (mLastMapFirst[_x, _y].power > 0)
            {
                GameRoot.DestroyImmediate(mLastMapObj[_x, _y]);
            }
            mLastMapFirst[_x, _y].power = Data;
            mLastMapFirst[x, y].power = 0;
            mLastMapObj[_x, _y] = Obj;
            GameManager.ChessControl.ClearnObjTag(false);
        }
    }
}
